﻿using DLLPC.Utilities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VisionCore;

namespace COER
{
    public partial class UC_Warning : UserControl
    {
        public UC_Warning()
        {
            InitializeComponent();
        }

        private List<LoadFileHelper.DataItem> AlarmDataItems = new List<LoadFileHelper.DataItem>();
        private List<LoadFileHelper.DataItem> WarningDataItems = new List<LoadFileHelper.DataItem>();
        public void ShowEnable()
        {
            AlarmDataItems = LoadFileHelper.LoadExcelFile("IOBlock.xlsx", "Alarm");
            WarningDataItems = LoadFileHelper.LoadExcelFile("IOBlock.xlsx", "Warning");
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            ShowAlarm();
            ShowWarning();

            if (lbxLog.Items.Count >0)
            {

            }
        }

        private void ShowAlarm()
        {
            string str = "";
            for (int i = 0; i < Kernel.EQ.AlarmNum; i++)
            {
                Monitor(Kernel.EQ.blnFs[i], AlarmDataItems[i], lbxLog);
                if (Kernel.EQ.blnFs[i]!= Kernel.EQ.blnFs_Buf[i])
                {
                    str = "Alarm " + (Kernel.EQ.blnFs[i] ? string.Format("Occur {0} {1} ", AlarmDataItems[i].Name, AlarmDataItems[i].Comment) : string.Format("Recovery {0} {1} ", AlarmDataItems[i].Name, AlarmDataItems[i].Comment));
                    if (Kernel.EQ.blnFs[i])
                    {
                        ProcedureHelper_EQ.StopBuzzer = false;
                    }
                    LogMsg(str);
                    Kernel.EQ.blnFs_Buf[i] = Kernel.EQ.blnFs[i];
                }
            }
        }

        private void ShowWarning()
        {
            string str = "";
            for (int i = 0; i < Kernel.EQ.WarningNum; i++)
            {
                Monitor(Kernel.EQ.blnWs[i], WarningDataItems[i], lbxLog);
                if (Kernel.EQ.blnWs[i] != Kernel.EQ.blnWs_Buf[i])
                {
                    str = "Warning " + (Kernel.EQ.blnWs[i] ? string.Format("Occur {0} {1} ", WarningDataItems[i].Name, WarningDataItems[i].Comment) : string.Format("Recovery {0} {1} ", WarningDataItems[i].Name, WarningDataItems[i].Comment));
                    if (Kernel.EQ.blnWs[i])
                    {
                        ProcedureHelper_EQ.StopBuzzer = false;
                    }
                    LogMsg(str);
                    Kernel.EQ.blnWs_Buf[i] = Kernel.EQ.blnWs[i];
                }
            }
        }

        private void Monitor(bool trig, LoadFileHelper.DataItem item,ListBox listBox)
        {
            string str = item.Name + " " + item.Comment;
            if (trig)
            {
                if (!listBox.Items.Contains(str))
                {
                    listBox.Items.Add(str);
                }
            }
            else
            {
                if (listBox.Items.Contains(str))
                {
                    listBox.Items.Remove(str);
                }
            }
        }

        private void LogMsg(string msg)
        {
            LogHelper.Write(LogHelper.Module.Alarm,msg );
        }
    }
}
